package com.park.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.park.dao.CarChannelRecordMapper;
import com.park.model.CarChannelRecord;
import com.park.model.Park;
import com.park.model.PosChargeData;
import com.park.service.CarChannelRecordService;
import com.park.service.JedisClient;


@Service
public class CarChannelRecordServiceImpl implements CarChannelRecordService{
	
	@Autowired
    CarChannelRecordMapper carChannelRecordMapper;
@Autowired 
JedisClient jedisClient;
	@Override
	public int insert(CarChannelRecord record) {
		// TODO Auto-generated method stub
		return carChannelRecordMapper.insert(record);
	}

	@Override
	public int insertSelective(CarChannelRecord record) {
		// TODO Auto-generated method stub
		return carChannelRecordMapper.insertSelective(record);
	}

	@Override
	public int carInRecord(Park park, PosChargeData posChargeData, Map<String, String> barrierRequestData,
			Map<String, Object> macInfo) {
		// TODO Auto-generated method stub
		  String description=park.getDescription()==null?"":park.getDescription();
	        if (!description.contains("出入记录")) {
	           return 0;
	        }
	        CarChannelRecord carChannelRecord=new CarChannelRecord();
	        carChannelRecord.setTime(new Date());
	        carChannelRecord.setCarNumber(barrierRequestData.get("cardNumber"));
	        carChannelRecord.setParkId(park.getId());
	        carChannelRecord.setChannelType((byte)0);
	        carChannelRecord.setCarNumberRedress(posChargeData.getCardNumber());
	        carChannelRecord.setChannelMac((String)macInfo.get("mac"));
	        carChannelRecord.setChannelName((String)macInfo.get("description"));
	        carChannelRecord.setActionType((byte) 0);
	        
	        int num=insertSelective(carChannelRecord);
	       	jedisClient.set("record-in"+posChargeData.getParkId()+posChargeData.getCardNumber(), String.valueOf(carChannelRecord.getId()), 300);
	        return num;
	}

	@Override
	public int carOutRecord(Park park, PosChargeData posChargeData, Map<String, String> barrierRequestData,
			Map<String, Object> macInfo, Byte type) {
		// TODO Auto-generated method stub
		 String description=park.getDescription()==null?"":park.getDescription();
	        if (!description.contains("出入记录")) {
	            return 0;
	        }
	        CarChannelRecord carChannelRecord=new CarChannelRecord();
	        carChannelRecord.setTime(new Date());
	        if (posChargeData==null) {
	        	carChannelRecord.setCarNumber(barrierRequestData.get("cardNumber"));
	 	        carChannelRecord.setChannelMac((String)macInfo.get("mac"));
	 	        carChannelRecord.setChannelType((byte)1);
	 	        carChannelRecord.setParkId(park.getId());
	 	        carChannelRecord.setChannelName((String)macInfo.get("description"));
	 	        carChannelRecord.setActionType(type);
	 	        carChannelRecord.setActionDescription("异常状态,没有找到入场记录");
	        }
	        else {
	        	 
	 	        carChannelRecord.setCarNumber(barrierRequestData.get("cardNumber"));
	 	        carChannelRecord.setCarNumberRedress(posChargeData.getCardNumber());
	 	        carChannelRecord.setChannelMac((String)macInfo.get("mac"));
	 	        carChannelRecord.setParkId(park.getId());
	 	        carChannelRecord.setChannelType((byte)1);
	 	        carChannelRecord.setChannelName((String)macInfo.get("description"));
	 	        carChannelRecord.setCharge((int)(posChargeData.getChargeMoney()*100));
	 	        carChannelRecord.setPaid((int)(posChargeData.getPaidMoney()*100));
	 	        carChannelRecord.setPayType((byte)posChargeData.getPayType());
	 	        carChannelRecord.setActionType(type);
	                   
	        }
	        int num=insertSelective(carChannelRecord);
	       	jedisClient.set("record-out"+park.getId()+barrierRequestData.get("cardNumber"), String.valueOf(carChannelRecord.getId()), 300);
	        return num;
	    }
	@Override
	public CarChannelRecord selectById(long id) {
		// TODO Auto-generated method stub
		return carChannelRecordMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateBySelective(CarChannelRecord record) {
		// TODO Auto-generated method stub
		return carChannelRecordMapper.updateByPrimaryKeySelective(record);
	}

	}


